package com.seventh.parkingrent.service.impl;

import com.seventh.parkingrent.controller.form.RentMessageFormForFront;
import com.seventh.parkingrent.dao.UserMapper;
import com.seventh.parkingrent.domain.RentMessage;
import com.seventh.parkingrent.dao.RentMessageMapper;
import com.seventh.parkingrent.service.IRentMessageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 订单详情记录表 服务实现类
 * </p>
 *
 * @author Lucas
 * @since 2021-08-25
 */
@Service
@Slf4j
public class RentMessageServiceImpl extends ServiceImpl<RentMessageMapper, RentMessage> implements IRentMessageService {
    @Resource
    private RentMessageMapper rentMessageMapper;
    @Resource
    private UserMapper userMapper;

    @Override
    public List<String> getLessor() {
        List<String> lessor = rentMessageMapper.getLessor();

        return lessor;
    }

    @Override
    public List<String> getTenantry() {

        List<String> tenantry=rentMessageMapper.getTenantry();
        return tenantry;
    }

    @Override
    public List<RentMessageFormForFront> getRentMessageByCondition(String status, String lessor, String tenantry, String rentStartTimeStart, String rentStartTimeEnd, String rentEndTimeStart, String rentEndTimeEnd, String propertyName) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date rentStartDateStart = null;
        Date rentStartDateEnd = null;
        Date rentEndDateStart = null;
        Date rentEndDateEnd = null;
        if(null!=rentStartTimeStart){
            String startTimeStart = rentStartTimeStart.substring(0,rentStartTimeStart.indexOf("T"));
            rentStartDateStart = format.parse(startTimeStart);
        }else{
            rentStartDateStart=format.parse("2021-08-01");
        }
        if(null!=rentStartTimeEnd){
            String startTimeEnd = rentStartTimeEnd.substring(0,rentStartTimeEnd.indexOf("T"));
            rentStartDateEnd = format.parse(startTimeEnd);
        }else{
            Date newDate = new Date();
            String s = format.format(newDate);
            String substring = s.substring(0, 10);
            rentStartDateEnd=format.parse(substring);
        }
        if(null!=rentEndTimeStart){
            String endTimeStart = rentEndTimeStart.substring(0,rentEndTimeStart.indexOf("T"));
            rentEndDateStart = format.parse(endTimeStart);
        }else{
            rentEndDateStart=format.parse("2021-08-01");
        }
        if(null!=rentEndTimeEnd){
            String endTimeEnd = rentEndTimeEnd.substring(0,rentEndTimeEnd.indexOf("T"));
            rentEndDateEnd = format.parse(endTimeEnd);
        }else{
            Date newDate = new Date();
            String s = format.format(newDate);
            String substring = s.substring(0, 10);
            rentEndDateEnd=format.parse(substring);
        }
        Integer lessorId=0;
        Integer tenantryId=0;
        if(null!=lessor){
           lessorId = userMapper.selectUserIdByUsername(lessor);

        }
        if(null!=tenantry){
            tenantryId = userMapper.selectUserIdByUsername(tenantry);

        }

        List<RentMessageFormForFront> forms = rentMessageMapper.selectRentMessageByCondition
                (status,lessorId,tenantryId,
                        rentStartDateStart,rentStartDateEnd,rentEndDateStart,
                        rentEndDateEnd,propertyName);
        for (RentMessageFormForFront form : forms) {
            Integer lesId = form.getLessorId();
            System.out.println("循环中获得出租方id："+lesId);
            String getLessor = userMapper.selectUsernameById(lesId);
            System.out.println("循环中获得出租方名字："+getLessor);
            form.setLessor(getLessor);

            Integer tenId = form.getTenantryId();
            System.out.println("循环中获得抢租客id："+tenId);
            String getTenantry = userMapper.selectUsernameById(tenId);
            System.out.println("循环中获得抢租客名字："+getTenantry);
            form.setTenantry(getTenantry);
        }

        return forms;
    }
}
